package com.zjm.creational.singleton.type07;

/**
 * 静态内部类（推荐）
 */
public class SingletonTest07 {
    public static void main(String[] args) {
        // 模拟10个线程并发获取单例对象
        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 获取到的实例：" + Singleton.getInstance());
            }, "线程-" + i).start();
        }
    }
}

class Singleton {
    // 构造器私有化
    private Singleton() {
    }

    // 静态内部类
    private static class SingletonInstance {
        private static final Singleton INSTANCE = new Singleton();
    }

    // 提供公共的静态方法，返回实例对象
    public static Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }
}
